//
//  BEMyOrdersCollectionCell.swift
//  BEGlobalOiler
//
//  Created by 柯平常 on 16/6/5.
//  Copyright © 2016年 stone. All rights reserved.
//

import UIKit

private let orderTableCellId = "orderTableCellId"

class BEMyOrdersCollectionCell: UICollectionViewCell {
    
    var pushOrderDetailVcBlock: ((_ orderId: Int64,_ techDataCount: Int,_ footerSn: Int) -> ())?
    
    var pushCreateOrderBlock: (() -> ())?
    
    var ordersListViewModel: BEMyOrdersListViewModel = BEMyOrdersListViewModel()
    
    
    var index: Int = 0{
        didSet{
            ordersListViewModel.ordersViewModelData.removeAll()
            loadData(orderStatus: index)
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setUpUI() {
        
        contentView.addSubview(tableView)
    }
    
    
    //下拉刷新方法
    @objc private func updateOrderData() {
        
    }
    
    
    private func loadData(orderStatus: Int){
        JFProgressHUD.showloading()
        showNetworkActivity()
        ordersListViewModel.loadOrderListData(orderStatus: orderStatus) { (success, tip) in
            //            self.tableView.mj_header.endRefreshing()
            JFProgressHUD.dismiss()
            if success {
                //                JFProgressHUD.showSuccessWithStatus(status: tip)
                DispatchQueue.main.async(execute: {
                    self.tableView.reloadData()
                })
            } else {
                JFProgressHUD.showInfoWithStatus(status: tip)
            }
        }
    }
    
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.frame = self.bounds
        tableView.backgroundColor = UIColor.colorWithHexString(hex: "f5f5f8")
        tableView.dataSource = self
        tableView.delegate = self
//        tableView.mj_header = setupHeaderRefresh(target: self, action: #selector(updateOrderData))
        
        tableView.register(UINib(nibName: "BEOrderTableCell", bundle: nil), forCellReuseIdentifier: orderTableCellId)
        
        tableView.rowHeight = 200
        //分割线
        tableView.separatorStyle = .none
        
        return tableView
    }()
}


extension BEMyOrdersCollectionCell: UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ordersListViewModel.ordersViewModelData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: orderTableCellId, for: indexPath) as! BEOrderTableCell
        cell.selectionStyle = .none
        cell.ordersViewModel = ordersListViewModel.ordersViewModelData[indexPath.row]
        cell.backgroundColor = UIColor.colorWithHexString(hex: "#f5f4f9")
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if ordersListViewModel.ordersViewModelData.count == 0{
            return
        }
        let orderViewModel = ordersListViewModel.ordersViewModelData[indexPath.row]
        switch orderViewModel.orderStatusName! {
        //push到创建订单控制器
        case "Saved":
            //            JFProgressHUD.showWithStatus(status: "Please continue to improve your order information!")
            //            DispatchQueue.global(qos: .background).asyncAfter(deadline: DispatchTime.now() + 1, execute: {
            //                JFProgressHUD.dismiss()
            //            })
            pushCreateOrderBlock!()
            
            //footerSn == 1时,无footer
            //        case "Customer Confirmed","In Progress","Completed":
            //            pushOrderDetailVcBlock!(orderId: ordersListViewModel.ordersViewModelData[indexPath.row].orderId!,techDataCount: ordersListViewModel.ordersViewModelData[indexPath.row].techCount!,footerSn: 1)
            
        //footerSn == 13时,加载支付footer
        case "Price Confirmed":
            //此处还未增加footer == true的判断和赋值
            //需要给block增加一个bool值
            pushOrderDetailVcBlock!(ordersListViewModel.ordersViewModelData[indexPath.row].orderId!,ordersListViewModel.ordersViewModelData[indexPath.row].techCount!,13)
        //确认footer
        case "Assigned":
            pushOrderDetailVcBlock!(ordersListViewModel.ordersViewModelData[indexPath.row].orderId!,ordersListViewModel.ordersViewModelData[indexPath.row].techCount!,0)
        //默认无footer
        default:
            pushOrderDetailVcBlock!(ordersListViewModel.ordersViewModelData[indexPath.row].orderId!,ordersListViewModel.ordersViewModelData[indexPath.row].techCount!,1)
        }
    }
    
    //动画效果
    //    func scrollViewDidScroll(scrollView: UIScrollView) {
    //
    //        let viewheight = scrollView.bounds.size.height + scrollView.contentInset.top
    //        for cell in tableView.visibleCells as! [BEOrderTableCell]{
    //            let y = cell.center.y - scrollView.contentOffset.y
    //            let p = y - viewheight / 2
    //            let scale = cos(p / viewheight * 0.6)
    //            UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
    //                cell.backimg.transform = CGAffineTransformMakeScale(scale, scale)
    //                }, completion: { (_) in
    //            })
    //        }
    //    }
    
}
